// address.js

import to_page from '../../../commons/pageaddress.js'
var requests = require('../../../requests/api.js');
var util = require('../../../utils/util.js');

var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectable: false,
    popuwindowHeight: 0,
    prompt: {
      hidden: !0,
      icon: '../../../assets/coupons/default-coupons.png',
      title: '暂无收货地址',
    },
    mainDataArray: [],
    isSelect: false
  },
  // 点击 默认 地址 icon
  selectAction(e) {
    var index = e.currentTarget.dataset.index;
    var obj = this.data.mainDataArray;
    var data = this.data;
    for (let i = 0; i < obj.length; i++) { // 遍历说当前点击的对象
      var address = obj[i];
      if (i == index) {
        if (!address.isDefault) {
          this.request_setDefaultAddress(address)
        }
      } else {
        address.isDefault = false
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const self = this
    new app.WeToast();
    wx.showLoading({
      title: '',
    })
    self.requestListData()
    if (options.selectable) {
      self.setData({
        selectable: options.selectable,
      })
    }

  },
  //请求网络 --  刷新列表数据
  requestListData: function() {
    const that = this;
    requests.get_addressList({}, (res) => {
      var arr = res.data.shippingInfoResultList
      for (var i = 0, l = arr.length; i < l; i++) {
        var obj = arr[i];
        obj.receiverMobilee = util.formatPhone(obj.receiverMobile);
      }
      this.setData({
        mainDataArray: arr
      })
    }, (data) => {
      if (util.alertNetworkError(data)) {
        that.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => {

      wx.stopPullDownRefresh()
      this.setData({
        'prompt.hidden': this.data.mainDataArray.length,
      })
    });
  },
  // 请求网络 -- 设置默认地址
  request_setDefaultAddress: function(obj) {
    const that = this;
    wx.showLoading({
      title: '',
    })
    requests.put_setDefaultAddress(obj.id, (res) => {
      obj.isDefault = !obj.isDefault // 改变数据选中状态 取反
      this.setData({
        mainDataArray: that.data.mainDataArray
      })
    }, (data) => {
      if (util.alertNetworkError(data)) {
        that.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => {

    });
  },
  toEditAddress: function(e) {
    var index = e.currentTarget.dataset.index;
    var obj = this.data.mainDataArray[index];
    var url = to_page.t + to_page.goEditAddress + '?title=编辑收货地址&isAdd=false&obj=' + JSON.stringify(obj);
    to_page.to(url)
  },
  toAddAddress: function(e) {

    var url = to_page.t + to_page.goEditAddress + '?title=添加收货地址&isAdd=true';
    to_page.to(url)

  },
  // 点击当前行的操作
  onSelect: function(e) {
    if (this.data.selectable) {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]
      prevPage.setData({
        address: e.currentTarget.dataset.data
      })
      wx.navigateBack()
    }
  },
  onPullDownRefresh: function() {
    this.requestListData()
  },
  /**
   * 当页面滑动到底部时触发的方法,删掉这个方法后控制台会报事件警告
   */
  onReachBottom: function() {},

})